<?php

namespace Modules\Admin\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Admin\Http\Requests\RoleRequest;
use Spatie\Permission\Models\Role;
class RoleController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
    	$roles=Role::where('name','!=','webmaster')->paginate(10);
        return view('admin::role.index',compact('roles'));
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create()
    {
        return view('admin::role.create');
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(RoleRequest $request)
    {
    	$post=$request->all();
    	Role::create($post);
    	session()->flash('success','添加角色成功！');
    	return redirect ()->route('role.index');
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {

    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit()
    {
        return view('admin::role.edit');
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(RoleRequest $request,$id)
    {
    	$role=Role::find($id);
    	$role->title=$request->input('title');
    	$role->name=$request->input('name');
    	$role->save();
    	session()->flash('success','角色修改成功！');
    	return redirect ()->route('role.index');
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy($id)
    {
    	Role::destroy ($id);
    	session()->flash('success','删除角色成功！');
    	return redirect ()->route('role.index');
    }
    public function showPermission($roleid){

		$permissions=\HDModule::getPermissionByGuard('admin');
		$role=Role::find($roleid);
    	return view('admin::permission.index',compact('permissions','role'));
	}
	public function addPermission(Request $request,$roleid){
		if(!$request->input('name')){
			session()->flash('danger','请至少选择一个权限！');
			return redirect ()->back();
		}
    	$role=Role::find($roleid);
    	$role->syncPermissions ($request->input('name'));
    	session()->flash('success','权限更改成功！');
    	return redirect ()->route('role.index');
	}

}
